
export class ComponentManager {

  protected static instance: ComponentManager;

  constructor() {
    return ComponentManager.instance || (ComponentManager.instance = this);
  }

  protected allComponents = {};

  setComponent(name: string, component: any) {
    this.allComponents[name] = component;
  }

  setComponents(components: { [key: string]: any }[]) {
    Object.assign(this.allComponents, components);
  }

  getComponent(name: string) {
    const comp = this.allComponents[name];
    if (!comp) {
      throw new Error(`${name} component is not found!`);
    }
    return this.allComponents[name]
  }
}

export const registerComponents = (comps: { [key: string]: any }[]) => {
  new ComponentManager().setComponents(comps);
}

export const getComponent = (name: string) => {
  return new ComponentManager().getComponent(name);
}